package databasetest;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Select extends JInternalFrame {
	DatabaseTest main;
	
//	Vector<String> col = new Vector<String>();
//	Vector<String> row = new Vector<String>();
//	DefaultTableModel model = new DefaultTableModel(col, 0);
	Connection conn;
	
	private JPanel panel;
	private JTextField q;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Create the frame.
	 */
	public Select(Connection c, DatabaseTest m) {
		super("조회", true, true, true, false);
		setBounds(100, 100, 450, 300);
		this.conn = c;
		this.main = m;
		
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		
	}
	
	public void execQuery(){
		String query = q.getText();
		
		try {
			String sql = query;
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();			
			
			int c = rsmd.getColumnCount();
			Vector<String> col = new Vector<>();
			for(int i=1; i<=c; i++){
				col.add(rsmd.getColumnName(i));
			}
			
			DefaultTableModel model = new DefaultTableModel(col, 0);
			table.setModel(model);
			
			while(rs.next()){
				Vector row = new Vector();
				for(int i=1; i<=c; i++){
					row.add(rs.getString(i));
				}
				model.addRow(row);
			}			
			pstmt.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getQ(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getQ() {
		if (q == null) {
			q = new JTextField();
			q.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						execQuery();
					}
				}
			});
			q.setColumns(10);
		}
		return q;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					execQuery();
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {	
		table = new JTable();	
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if(main.update == null){
					main.openUpdate();
				}
				int row = table.getSelectedRow();
				String s = ""+table.getValueAt(row, 1);
				String c = ""+table.getValueAt(row, 2);
				String ea = ""+table.getValueAt(row, 3);				
				
				main.update.setData(s,c,ea);
			}
		});
		
		}
		return table;
	}
}
